package br.com.massai.model.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import br.com.massai.model.entity.Collaborator;
import br.com.massai.model.exceptions.SystemException;
import br.com.massai.view.enums.Status;

@Stateless
public class CollaboratorDAO extends GenericDAO<Collaborator> implements CollaboratorRepository {

	private static final long serialVersionUID = 2266969099369410142L;

	public CollaboratorDAO() {
		super(Collaborator.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Collaborator> getCollaboratorsActive() throws SystemException {
		return em.createQuery("SELECT c FROM Collaborator c WHERE c.active =:active").setParameter("active", Status.A).getResultList();
	}

	@Override
	public Collaborator login(Collaborator collaborator) throws SystemException {
		try {
			Query query = em.createNamedQuery("Collaborator.login").setParameter("username", collaborator.getUsername()).setParameter("password", collaborator.getPassword()).setParameter("active", Status.A);
			return (Collaborator) query.getSingleResult();

		} catch (NoResultException e) {
			e.printStackTrace();
			return null;
		}
	}
}